﻿using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using SqlSugar;
using Util;

namespace WebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class BlogController : ControllerBase
    {
        private readonly IBlogService _service;
        private readonly ILogger<BlogController> _logger;

        public BlogController(
            IBlogService service,
            ILogger<BlogController> logger)
        {
            _service = service;
            _logger = logger;
        }

        [HttpGet("Get")]
        public async Task<ActionResult<ApiResult>> Get()
        {
            _logger.LogInformation("LogInformation！");
            _logger.LogWarning("LogWarning！");
            _logger.LogError("LogError！");
            _logger.LogCritical("LogCritical");
            _logger.LogTrace("LogTrace");
            var blogs =  await _service.QueryAsync();
            return ApiResultHelper.Success(blogs, blogs.Count, "获取成功");
            _logger.LogDebug("LogDebug");
        }

        [HttpPost("Create")]
        public async Task<ActionResult<ApiResult>> Create(string title,string content, int dictionaryId)
        {
            Blog b = new Blog
            {
                Content = content,
                Title = title,
                ReadCount = 0,
                UserId = 1,
                DictionaryId = dictionaryId
            };
            var result = await _service.CreateAsync(b);
            return ApiResultHelper.Success(b);
        }

        [HttpGet("GetPage")]
        public async Task<ActionResult<ApiResult>> GetPage(int size, int page)
        {
            RefAsync<int> total = 0;
            var blogs = await _service.QueryPageAsync(size,page,total);
            return ApiResultHelper.Success(blogs, blogs.Count, "获取成功");
        }
    }
}
